package com.molichuxing.services.infrastructure.service.impl;

import com.alibaba.fastjson.JSON;
import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.services.infrastructure.bean.LoginLogBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.LoginLogMapper;
import com.molichuxing.services.infrastructure.dto.response.request.create.LoginLogCreateDto;
import com.molichuxing.services.infrastructure.dto.response.response.LoginLogDto;
import com.molichuxing.services.infrastructure.service.LoginLogService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 系统操作日志表 服务实现类
 * @author xuelin yi
 * @since 2019-09-20
 */
@Service("loginLogService")
public class LoginLogServiceImpl implements LoginLogService {

    private static final Logger log = Logger.getLogger(LoginLogServiceImpl.class);

    @Resource
    private LoginLogMapper loginLogMapper;


    @Override
    public Paged<LoginLogDto> getLoginLogByDealerId(Map<String, Object> map, Integer pageNum, Integer pageSize) {
        int count = loginLogMapper.getLoginLogCount(map);
        if(count <= 0) {
            return new Paged<>();
        }
        List<LoginLogBean> pageResult = loginLogMapper.getLoginLogByDealerId(map, pageNum, pageSize);
        log.info("根据条件查询登陆列表 出参：pageResult： "+ JSON.toJSONString(pageResult));
        return new Paged<>(Convert.toLoginLogBeanDto(pageResult), count, pageSize );
    }

    @Deprecated
    @Override
    public int createLoginLog(LoginLogDto dto) {
        if (dto == null) {
            throw new NullPointerException("dto is null");
        }
        return loginLogMapper.create(Convert.toLoginLogBean(dto));

    }

    /**
     * 新增登录（登出）日志
     *
     * @param createDto
     * @return
     */
    @Override
    public Boolean create(LoginLogCreateDto createDto) {
        if (createDto == null) {
            throw new NullPointerException("createDto不能为空");
        }
        return loginLogMapper.create(Convert.toLoginLogBean(createDto)) > 0 ? true : false;
    }

    @Override
    public LoginLogDto getAccountByLastLoginTime(int accountId, int type) {
        if (accountId <= 0) {
            throw new NullPointerException("param invalid accountId is null");
        }
        return Convert.toLoginLogBeanDto(loginLogMapper.getAccountLastTimeByIdAndType(accountId, type));
    }

    @Override
    public List<LoginLogDto> getLogByAccountAndType(String account, Integer type) {
        return Convert.toLoginLogBeanDto(loginLogMapper.getLogByAccountAndType(account, type));
    }

    @Override
    public int updateAccountLogOutTime(String account) {
        return loginLogMapper.updateAccountLogOutTime(account);
    }

    /**
     * 获取登录日志信息
     *
     * @param params
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Paged<LoginLogDto> getLoginLog(Map<String, Object> params, Integer pageNum, Integer pageSize) {
        // 开始时间-LocalDateTime
        params.put("loginTimeStart", ObjectCastUtil.castLocalDateTimeStart(params.get("loginTimeStart")));
        // 结束时间-LocalDateTime
        params.put("loginTimeEnd", ObjectCastUtil.castLocalDateTimeEnd(params.get("loginTimeEnd")));

        long total = loginLogMapper.getPageCount(params);
        List<LoginLogBean> page = null;
        if (total > 0) {
            page = loginLogMapper.getPage(params, pageNum, pageSize);
        }

        return new Paged<LoginLogDto>(Convert.toLoginLogDto(page), total, pageSize);
    }
}


